//
//  RankingListViewController.m
//  NewPro
//
//  Created by RYX on 2019/1/8.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "RankingListViewController.h"
#import "RankingListOneViewController.h"
#import "RankingListTwoViewController.h"
#import "SMPagerTabView.h"

@interface RankingListViewController ()<SMPagerTabViewDelegate>

@property (nonatomic, strong) NSMutableArray *vcArr;
@property (nonatomic, strong) SMPagerTabView *segmentView;
@property (nonatomic, copy) NSString *seletime;

@end

@implementation RankingListViewController

- (void)onRightBtn:(UIButton *)sender{
    
    NSDate *minDate = [NSDate setYear:1978 month:3 day:12];
    NSDate *maxDate = [NSDate date];
    
    MJWeakSelf
    [BRDatePickerView showDatePickerWithTitle:@"选择日期" dateType:BRDatePickerModeYM defaultSelValue:weakSelf.seletime minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
        weakSelf.seletime = selectValue;
        NSArray *listArr = [selectValue componentsSeparatedByString:@"-"];
        NSString *mon = [NSString stringWithFormat:@"%@月",listArr.lastObject];
        [sender setTitle:mon forState:UIControlStateNormal];
        
        NSDictionary *dic = @{
                              @"currentDate":selectValue
                              };
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectDate" object:nil userInfo:dic];
        
    } cancelBlock:^{
        
        
    }];
}

- (void)setNavBar{
    
    [self setNavBarWithTitle:@"排行榜"];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //修改按钮向左偏移10 point
    [rightBtn setFrame:CGRectMake(10.0, 0.0, 65.0, 44.0)];
    [rightBtn addTarget:self action:@selector(onRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:[DateManager getCurrentMonth] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    //修改方法
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 65.0, 44.0)];
    [view addSubview:rightBtn];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBar];
    
    [self setUI];
}

- (void)setUI{
    
    _vcArr = [NSMutableArray array];
    
    RankingListOneViewController *vc1 = [[RankingListOneViewController alloc] init];
    vc1.title = @"交易总额";
    
    RankingListTwoViewController *vc2 = [[RankingListTwoViewController alloc]initWithNibName:nil bundle:nil];
    vc2.title = @"激活总数";
    
    [_vcArr addObject:vc1];
    [_vcArr addObject:vc2];

    self.segmentView.delegate = self;
    
    //可自定义背景色和tab button的文字颜色等
    //开始构建UI
    [_segmentView buildUI];
    
    //起始选择一个tab
    [_segmentView selectTabWithIndex:0 animate:NO];
    
}

#pragma mark - DBPagerTabView Delegate
- (NSUInteger)numberOfPagers:(SMPagerTabView *)view {
    return [_vcArr count];
}
- (UIViewController *)pagerViewOfPagers:(SMPagerTabView *)view indexOfPagers:(NSUInteger)number {
    return _vcArr[number];
}

#pragma mark - setter/getter
- (SMPagerTabView *)segmentView {
    if (!_segmentView) {
        self.segmentView = [[SMPagerTabView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:_segmentView];
        
        [self.segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.right.bottom.equalTo(self.view);
        }];
    }
    return _segmentView;
}

@end
